package com.psy.counseling.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.psy.counseling.entity.Ask;
import com.psy.counseling.service.AskService;
import com.psy.counseling.utils.Result;
import com.psy.counseling.vo.AskVO;
import com.psy.counseling.vo.DeleteVO;
import com.psy.counseling.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 咨询
 *
 * @author 陈兴海
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/api/ask")
public class AskController {

    @Autowired
    private AskService askService;

    /**
     * 获取咨询预约列表
     *
     * @param ask
     * @param page
     * @return
     */
    @GetMapping("/listAsks")
    public Result<Page<Ask>> getAskList(Ask ask, PageVO page) {
        if (ask == null) {
            return Result.error("204", "查询失败");
        }
        Page asks = askService.listAsks(ask, page);
        return Result.success(asks);
    }

    /**
     * 通过学生id获取咨询信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getAskRecord/{id}")
    public Result<List<Ask>> getAskRecord(@PathVariable Long id) {
        List<Ask> ask = askService.getAskRecord(id);
        if (ask == null) {
            return Result.error("204", "查询失败");
        }
        return Result.success(ask);
    }

    /**
     * 通过id删除咨询记录
     *
     * @param deleteVO 需要删除的id
     * @param flag     标记是批量删除还是单个删除
     * @return
     */
    @DeleteMapping("/deleteAskRecord/{flag}")
    public Result deleteAskRecord(@RequestBody DeleteVO deleteVO, @PathVariable("flag") Integer flag) {
        return askService.deleteAskRecord(deleteVO, flag);
    }

    /**
     * 通过id修改咨询记录
     *
     * @param ask
     * @return
     */
    @PutMapping("/updateAskRecord")
    public Result updateAskRecord(@RequestBody Ask ask) {
        if (ask != null) {
            askService.updateById(ask);
            return Result.success();
        }
        return Result.error("204", "修改失败");
    }

    /**
     * 咨询申请
     *
     * @param ask
     * @return
     */
    @PostMapping("/addAsk/{userId}")
    public Result addAsk(@RequestBody Ask ask,@PathVariable Long userId) {
//        long userId = StpUtil.getLoginIdAsLong();
        return askService.addAsk(ask, userId);
    }


    /**
     * 追加咨询时间时间
     *
     * @param askVO
     * @return
     */
    @PutMapping("/addAskTime")
    public Result<?> addAskTime(@RequestBody AskVO askVO) {
        return askService.addAskTime(askVO);
    }

    /**
     * 提前中止
     *
     * @param askVO
     * @return
     */
    @PutMapping("/suspendAsk")
    public Result suspendAsk(@RequestBody AskVO askVO) {
        return askService.suspendAsk(askVO);
    }

    /**
     * 拒绝申请
     *
     * @param id
     * @return
     */
    @PutMapping("/rejectAsk/{id}")
    public Result<?> rejectAsk(@PathVariable("id") Long id) {
        return askService.rejectAsk(id);
    }

    /**
     * 审核通过
     *
     * @param ask
     * @return
     */
    @PutMapping("/viaAsk")
    public Result<?> viaAsk(@RequestBody Ask ask) {
        return askService.viaAsk(ask);
    }

    /**
     * 获取审核通过后的，咨询状态
     *
     * @param id
     * @return
     */
    @GetMapping("/status/{id}")
    public Result<?> getStatus(@PathVariable("id") Long id) {
        return askService.getStatus(id);
    }

    /**
     * 取消预约
     *
     * @param id
     * @return
     */
    @PutMapping("/cancelAsk/{id}")
    public Result<?> cancelAsk(@PathVariable("id") Long id) {
        return askService.cancelAsk(id);
    }

}

